package com.siwei.mes.controller.materials;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.entity.materials.Materials;
import com.siwei.mes.entity.materials.MaterialsTestProject;
import com.siwei.mes.mapper.materials.MaterialsTestProjectMapper;
import com.siwei.mes.service.materials.MaterialsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 物料表相关
 *
 * @author linzi
 * @since 2023-11-27 22:46:48
 */
@Api(value = "物料表")
@RestController
@RequestMapping("materials")
public class MaterialsController {
    @Resource
    private MaterialsService materialsService;
    @Resource
    private MaterialsTestProjectMapper materialsTestProjectMapper;

    @ApiOperation(value = "分页获取物料信息")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<Materials, Materials> query) {
        return Json.succ().data(materialsService.getPage(query));
    }

    @ApiOperation(value = "分页获取物料信息")
    @PostMapping("/getAll")
    public Json getAll(@RequestBody Materials entity) {
        return Json.succ().data(materialsService.getAll(entity));
    }

    /**
     * 设置材料及规格配置信息
     *
     * @param entity
     * @return
     */
    @ApiOperation(value = "设置材料及规格配置信息")
    @PostMapping("config")
    public Json config(@RequestBody Materials entity) {
        assertTrue(entity.getId() == null, "物料主键id为空");
        assertTrue(entity.getSpecId() == null, "材料及规格配置id为空");
        materialsService.configSpecId(entity);
        return Json.succ().data(new ApiResult());
    }

    /**
     * 绑定配合比
     *
     * @param entity
     * @return
     */

    @ApiOperation(value = "绑定配合比")
    @PostMapping("bindPhbNo")
    public Json bind(@RequestBody Materials entity) {
        assertTrue(entity.getId() == null, "主键id为空");
        assertTrue(StringUtils.isBlank(entity.getPhbNo()), "配合比编号为空");
        materialsService.bindPhbNo(entity.getId(), entity.getPhbNo());
        return Json.succ().data(new ApiResult());
    }

    /**
     * 根据物料类型获取所有物料名称  可以不传
     *
     * @param materialType 物料类型 字典 MATERIAL_TYPE
     * @return
     */
    @GetMapping("getMaterialsNameByType")
    public Json getMaterialsName(@RequestParam(required = false, name = "materialType") Long materialType) {
        return Json.succ().data(ApiResult.addList(materialsService.getMaterialsNameByType(materialType)));
    }

    /**
     * 根据物料名称获取对应的物料规格
     *
     * @param materialName 物料名称
     * @param materialType 物料类型 字典 MATERIAL_TYPE
     * @return
     */
    @GetMapping("getMaterialsSpecs")
    public Json getMaterialsSpecs(@RequestParam("materialName") String materialName,
                                  @RequestParam(required = false, name = "materialType") Long materialType) {
        return Json.succ().data(ApiResult.addList(materialsService.getMaterialsSpecs(materialName, materialType)));
    }

    /**
     * 根据物料类型获取对应的试验项目
     *
     * @param materialType 物料类型 字典 MATERIAL_TYPE
     * @return
     */
    @GetMapping("getTestProject")
    public Json getTestProject(@RequestParam("materialType") Long materialType) {
        MaterialsTestProject entity = new MaterialsTestProject();
        entity.setMaterialType(materialType);
        List<MaterialsTestProject> all = materialsTestProjectMapper.getList(entity);
        return Json.succ().data(ApiResult.addList(all));
    }

    @ApiOperation(value = "删除物料表数据")
    @PostMapping("/delete")
    public Json delete(@RequestBody Materials entity) {
        materialsService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }
}
